package JPetStore.persistence.impl;

import JPetStore.domain.Category;
import JPetStore.domain.Language;
import JPetStore.persistence.DBUtil;
import JPetStore.persistence.LanguageDao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class LanguageDaoImpl implements LanguageDao {

    private static final String GET_CATEGORY_LIST =
            "SELECT LANGUAGEID AS languageId, LANGUAGES AS languages FROM LANGUAGE";

    @Override
    public List<Language> getLanguageList() {
        List<Language> languageList = new ArrayList<>();
        try {
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(GET_CATEGORY_LIST);
            while(resultSet.next()){
                Language language = new Language();
                language.setLanguageId(resultSet.getString("languageId"));
                language.setLanguages(resultSet.getString("languages"));
                languageList.add(language);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return languageList;
    }
}
